#ifndef SERIAL_H
#define SERIAL_H

#include <QObject>
#include<QtSerialPort/QSerialPort>
#include <QSerialPortInfo>

class Serial : public QObject
{
    Q_OBJECT
public:
    //数据处理
    static QString HexToHexstr(const QByteArray hex);
    static QByteArray HexstrToByte(QString str);
    static unsigned int CRC_16(unsigned char data1[], unsigned char Length);
    static QString strHex2Dec(QString hexStr);
    static QString strDec2Hex(QString decStr);

    static Serial *instance();

    //查找可用串口
    static QStringList findPort();
    //获取支持的波特率
    static QStringList findBaudRate();
    //打开串口
    bool open(QString port, quint64 baud = 9600);
    //关闭串口
    void close(){   serial->close();    }
    //串口是否打开
    bool isOpen(){  return serial->isOpen();    }

    //发送数据
    qint64 sendData(QByteArray data);
    qint64 sendData(QString data);

signals:
    void haveReceive(QByteArray data);
    void sendDataShow(QString data,bool isOk);  //用于显示，isOK表示发送成功或失败

private:
    static Serial* mPtr;
    explicit Serial(QObject *parent = nullptr);
    QSerialPort *serial;

};


#endif // SERIAL_H

